package com.xuedian.xuanxuan.web.controller;


import com.xuedian.xuanxuan.core.entity.JSONResult;
import com.xuedian.xuanxuan.core.util.HtmlUtil;
import com.xuedian.xuanxuan.core.util.TimeUtils;
import com.xuedian.xuanxuan.web.model.RLogin;
import com.xuedian.xuanxuan.web.model.Token;
import com.xuedian.xuanxuan.web.model.User;
import com.xuedian.xuanxuan.web.service.MUserService;
import com.xuedian.xuanxuan.web.service.TokenService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Controller
@RequestMapping("/user")
public class MUserController {

    @Resource
    private MUserService mUserService;
    @Resource
    private TokenService tokenService;

    /**
     * 登录
     * @param openId 第三方ID
     * @param type 类型：0是微信登录，1是qq登录
     * @param gender 性别：0未知，1男，2女
     * @param avatar 头像
     * @param nick 昵称
     * @return
     * {"code": "code",
        "msg": "errorMsg",
        "data":
        {
        "token":"1","userId":"1"
        }
        }
     */
    @RequestMapping(value = "/getLogin", method = RequestMethod.POST, produces="application/json")
    public String getLogin(HttpServletRequest request, HttpServletResponse response, @RequestParam String openId, @RequestParam int type, @RequestParam int gender, @RequestParam String avatar, @RequestParam String nick) {
        JSONResult jsonResult;
        RLogin rLogin = null;
        int code = 2000;
        int res = 1;
        if (StringUtils.isEmpty(openId)){
            code = 3000;
        }else {
            User user = mUserService.selectByOpenId(openId,type);
            if (user!=null){
                //更新用户信息
                user.setGender(gender);
                user.setAvatar(avatar);
                user.setNick(nick);
                user.setUpdateTime(TimeUtils.getTime());
                res = mUserService.update(user);
                if (res<=0){
                    code = 3004;
                }
            }else {
                //增加新用户
                user = new User();
                if (type==0){
                    user.setWxId(openId);
                }
                user.setGender(gender);
                user.setAvatar(avatar);
                user.setNick(nick);
                user.setState(0);
                user.setCreateTime(TimeUtils.getTime());
                user.setUpdateTime(TimeUtils.getTime());
                int id = mUserService.insert(user);
                res = id;
                if (id>0){
                    user.setId((long)id);

                }
            }
            if (res>0){
                //生成token并返回
                Date currentDate = TimeUtils.getTime();
                String tk = ""+ TimeUtils.getTimestamp();
                Token token = tokenService.selectByUserId(user.getId());
                if (token!=null){
                    token.setOperationTime(currentDate);
                    token.setToken(tk);
                    res = tokenService.update(token);
                }else {
                    token = new Token(user.getId(),currentDate);
                    token.setToken(tk);
                    res = tokenService.insert(token);
                }
                rLogin = new RLogin(user.getId(),tk);
            }

            if (res<=0){
                code = 3004;
            }

        }

        jsonResult = new JSONResult(rLogin,code);
        HtmlUtil.writerJson(response,jsonResult);
        return "";
    }


}